﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Telerik.Windows.Controls;
using Baosight.Platform.Core.UI.Controls;
using Baosight.Platform.Core;
using Baosight.ZNH.ZMCommon;
using Baosight.Platform.Core.ServiceModel;

namespace Baosight.ZNH.ZM.UI.Controls
{
    public partial class HomeContainer : UserControl
    {
        private FrameworkElement element = Application.Current.RootVisual as FrameworkElement;
        private int titleHeight = 70;
        public HomeContainer()
        {
            InitializeComponent();
            this.MinHeight = element.ActualHeight - titleHeight - 2;
            element.SizeChanged += (a, b) =>
                {
                    this.MinHeight = b.NewSize.Height - titleHeight - 2;
                };
        }

        private void HyperlinkButton_Click(object sender, RoutedEventArgs e)
        {
            HyperlinkButton link = sender as HyperlinkButton;
            link.IsEnabled = false;
            EIRow row = link.ParentOfType<RadTileViewItem>().Header as EIRow;
            if (row["fdHyperlinkViewName"] == null)
            {
                BaseTools.OpenEFWindowAlert("没有找到跳转页面号！", null);
                link.IsEnabled = true;
                return;
            }
            ZM90 zm90 = this.ParentOfType<ZM90>();
            zm90.LinkView(row["fdHyperlinkViewName"] as string);
            link.IsEnabled = true;
        }

        /// <summary>
        //点击保存后的回调函数
        /// </summary>
        private void DoWorkCompleted(object sender, DoWorkCompletedEventArgs e)
        {
            if (e.EIInfo.GetStatus() == -1)
            {
                EFWindow.Alert("更新数据失败！");
            }
        }


        private void myTileView_TileDragEnded(object sender, TileViewDragEventArgs e)
        {
            EIInfo info = new EIInfo();
            EIBlock updateBlock = new EIBlock("result");
            EIColumn columnValue = new EIColumn();
            columnValue.SetName("fdValue");
            updateBlock.AddColumn(columnValue);

            EIColumn columnUserId = new EIColumn();
            columnUserId.SetName("fdUserId");
            updateBlock.AddColumn(columnUserId);

            EIColumn columnKeyId = new EIColumn();
            columnKeyId.SetName("fdKeyId");
            updateBlock.AddColumn(columnKeyId);
            foreach (object item in myTileView.Items)
            {
                RadTileViewItem rr = item as RadTileViewItem;
                EIRow row = rr.Header as EIRow;
                row["fdValue"] = rr.Position;
                updateBlock.AddRow(row);
            }
            info.Attributes[EIConstants.ServiceNameKey] = "ZM90";
            info.Attributes[EIConstants.MethodNameKey] = "update";
            info.AddBlock(updateBlock);
            HttpServiceAgent agent = new HttpServiceAgent();
            agent.CallService(info, DoWorkCompleted);
        }

    }
}
